﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace employee.Controls
{
    public partial class emWorkPunishment : common.controls.baseListEdit
    {
        private data.baseDS.emWorkRewardDataTable myDataTbl = new data.baseDS.emWorkRewardDataTable();
        public emWorkPunishment()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception er)
            {
                ErrorHandler(this, er);;
            }            
        }
        public override event common.myEventHandler myOnError
        {
            add
            {
                base.myOnError += value;
                detailObj.myOnError += value;
            }
            remove
            {
                base.myOnError -= value;
                detailObj.myOnError -= value;
            }
        }
        public override void Init()
        {
            base.Init();
            detailObj.Init();
            SetData(myDataTbl);
        }
        public override void LoadData(string emCode) 
        {
            myDataTbl.Clear();
            if (emCode != null)
            {
                DataAccess.DataLibs.LoadData(myDataTbl,detailObj.myRewardCat, emCode);
            }
        }
        public override void SaveData(string emCode)
        {
            gridDataSource.EndEdit();
            for (int idx = 0; idx < myDataTbl.Count; idx++)
            {
                if (myDataTbl[idx].RowState == DataRowState.Deleted) continue;
                myDataTbl[idx].category = detailObj.myRewardCat;
                myDataTbl[idx].emCode = emCode;
            }
            DataAccess.DataLibs.UpdateData(myDataTbl);
        }
        public override void LockEdit(bool state)
        {
            base.LockEdit(state);
            detailObj.LockEdit(state);
        }
        public override bool CheckData()
        {
            return detailObj.CheckData();
        }
        public override void SetFocus()
        {
            detailObj.SetFocus();
        }
        public override void AddNew()
        {
            data.baseDS.emWorkRewardRow row;
            gridDataSource.AddNew();
            if (this.gridDataSource.Current != null)
            {
                row = (data.baseDS.emWorkRewardRow)((DataRowView)this.gridDataSource.Current).Row;
                DataAccess.DataLibs.InitData(row);
            }
            detailObj.SetDataSource(gridDataSource);
        }
        private void SetData(data.baseDS.emWorkRewardDataTable tbl)
        {
            gridDataSource.DataSource = tbl;
            detailObj.SetDataSource(gridDataSource);

            DataGridViewTextBoxColumn titleColumn = new DataGridViewTextBoxColumn();
            
            this.detailGrid.Columns.AddRange(new DataGridViewColumn[] 
                                        {
                                            titleColumn
                                        }
                                         );
            // 
            // titleColumn
            // 
            titleColumn.DataPropertyName = tbl.tittleColumn.ColumnName;
            titleColumn.HeaderText = "Tiêu đề";
            titleColumn.Name = tbl.tittleColumn.ColumnName;
            titleColumn.ReadOnly = true;

            detailGrid.myAutoFitColumn = titleColumn.Name;
            detailGrid.AutoFit();
        }
    }
}
